package nl.hanze.designpatterns.mvc.gui;

import java.awt.Dimension;
import java.util.HashMap;
import java.util.Map;

import nl.hanze.designpatterns.domain.LoginCredential;
import nl.hanze.designpatterns.mvc.controller.DashboardController;
import nl.hanze.designpatterns.mvc.controller.ThesaurusController;
import nl.hanze.designpatterns.mvc.controller.TroubleTicketController;
import nl.hanze.designpatterns.mvc.model.DashboardModel;
import nl.hanze.designpatterns.mvc.model.ThesaurusModel;
import nl.hanze.designpatterns.mvc.model.TroubleTicketModel;
import nl.hanze.designpatterns.mvc.view.ThesaurusView;
import nl.hanze.designpatterns.mvc.view.TroubleTicketAddFormView;
import nl.hanze.designpatterns.mvc.view.TroubleTicketView;
import nl.hanze.designpatterns.mvc.view.View;
import nl.hanze.designpatterns.mvc.view.menu.DashboardMenu;

public class DashboardGUI extends GUI {
	
	public static final String TROUBLE_TICKET_OVERVIEW = "troubeTicketOverview";	
	public static final String TROUBLE_TICKET_ADD = "troubleTicketAddView";
	public static final String THESAURUS = "thesaurus";
	
	private View currentView;
	
	private Map<String, View> views;
	
	private DashboardMenu menu;
	private DashboardController guiController;
	private DashboardModel guiModel;
	
	private TroubleTicketController troubleTicketController;
	private TroubleTicketModel troubleTicketModel;
	private TroubleTicketView troubleTicketView;
	
	private ThesaurusController thesaurusController;
	private ThesaurusModel thesaurusModel;
	private ThesaurusView thesaurusView;
	
	private LoginCredential user;
	
	public DashboardGUI(LoginCredential user) {
		this.views = new HashMap<String, View>();
		
		this.user = user;
		
		this.guiModel = new DashboardModel();
		this.guiController = new DashboardController(guiModel, this);
		this.menu = new DashboardMenu(guiController);
		
		this.troubleTicketModel = new TroubleTicketModel();
		this.troubleTicketController = new TroubleTicketController(troubleTicketModel);
		this.troubleTicketView = new TroubleTicketView(troubleTicketController, troubleTicketModel);
		this.addView(TROUBLE_TICKET_ADD, new TroubleTicketAddFormView(troubleTicketController, troubleTicketModel));
		this.addView(TROUBLE_TICKET_OVERVIEW, this.troubleTicketView);
		this.troubleTicketModel.addView(TROUBLE_TICKET_OVERVIEW, this.troubleTicketView);
		
		this.thesaurusModel = new ThesaurusModel();
		this.thesaurusController = new ThesaurusController(thesaurusModel);
		this.thesaurusView = new ThesaurusView(thesaurusController, thesaurusModel);
		this.addView(THESAURUS, this.thesaurusView);
		this.thesaurusModel.addView("thesaurus", this.thesaurusView);
		
		this.setTitle("Dashboard - " + user.getUserName());
		
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setResizable(false);
		
		Dimension d = new Dimension(800, 600);
		this.setSize(d);
		this.setPreferredSize(d);
		
		this.setJMenuBar(menu);
		
		this.setVisible(true);
		
		this.setLocationRelativeTo(null);
	}
	
	private void setCurrentView(View view){
		this.currentView = view;
	}
	
	private View getCurrentView(){
		return this.currentView;
	}
	
	private void addView(String viewID, View view){
		this.views.put(viewID, view);
	}
	
	private View getView(String viewID){
		return this.views.get(viewID);
	}

	public void loadView(String viewID){
		View newView = this.getView(viewID);
		if(newView != null){
			if(this.getCurrentView() != null){
				this.remove(this.getCurrentView());
				this.getCurrentView().setVisible(false);
			}
			this.setCurrentView(newView);
			add(newView);
			newView.setVisible(true);
			this.validate();
			System.out.println("View changed!");
		}
	}
	
	public String getLoggedInUserName(){
		return this.user.getUserName();
	}
	
}
